package game.module.ranking.dao;

import game.module.ranking.bean.RankingHero;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class RankingDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	private RowMapper<RankingHero> rankingHeroMapper = new CharacterRowMapper();

	public List<RankingHero> refreshGradeRanking() {
		return jdbcTemplate.query("select f_id,f_name,f_grade,f_copper from t_character order by f_grade desc limit 100", rankingHeroMapper);
	}

	public List<RankingHero> refreshCopperRanking() {
		return jdbcTemplate.query("select f_id,f_name,f_grade,f_copper from t_character order by f_copper desc limit 100", rankingHeroMapper);
	}

	public static final class CharacterRowMapper implements RowMapper<RankingHero> {
		@Override
		public RankingHero mapRow(ResultSet rs, int rowNum) throws SQLException {
			RankingHero rh = new RankingHero();
			rh.setId(rs.getInt("f_id"));
			rh.setName(rs.getString("f_name"));
			rh.setGrade(rs.getInt("f_grade"));
			rh.setCopper(rs.getInt("f_copper"));
			return rh;
		}
	}

}
